using System;
using NUnit.Framework;

namespace parts.tests
{
    [TestFixture]
    public class PartCanBeAMethodTests
    {
        [Test]
        public void Static_method() {
            var part = new Part<string, int>(StringUtilsAsMethods.StaticLength);
            var result = 0;
            part.Result += x => result = x;
            part.Process("Hallo");
            Assert.That(result, Is.EqualTo(5));
        }

        [Test]
        public void Instance_method() {
            var part = new Part<string, int>(x => new StringUtilsAsMethods().InstanceLength(x));
            var result = 0;
            part.Result += x => result = x;
            part.Process("Hallo");
            Assert.That(result, Is.EqualTo(5));
        }

        [Test]
        public void Class() {
            var part = new Part<string, int, StringUtilsAsClass>();
            var result = 0;
            part.Result += x => result = x;
            part.Process("Hallo");
            Assert.That(result, Is.EqualTo(5));
        }


    }

    public class StringUtilsAsMethods
    {
        public static int StaticLength(string s) {
            return s.Length;
        }

        public int InstanceLength(string s) {
            return s.Length;
        }

    }

    public class StringUtilsAsClass : IPart<string, int>
    {
        public void Process(string pairs) {
            Result(pairs.Length);
        }

        public event Action<int> Result;
    }
}